package game.gamestate.title;

import engine.Constants;
import engine.Engine;
import engine.filesystem.ResourceManager;
import engine.game.Game_I;
import game.gamestate.GameState;
import game.gamestate.title.ui.MainMenu;
import game.resource.loader.ResourceLoader;

public class Title implements Game_I {
	public Title() {
		State = new GameState();
	}
	
	public GameState State;
	private int _FrameCounter;
	@Override
	public boolean initialize(Engine engine) {
		loadResources();
		generateObjects();
		return true;
	}

	@Override
	public boolean execute(Engine engine, int elapsed) {
		
		// Input
		State.Keyboard.sendMessage(engine.getKeyboardQueue());

		// Timing
		_FrameCounter += elapsed;
		while(_FrameCounter > Constants.FRAME_TIME) {
			// Entity processing
			State.Entities.process(State, Constants.FRAME_TIME);
			
			_FrameCounter -= Constants.FRAME_TIME;
		}
		
		// Rendering
		engine.getScreenBuffer();
		State.Scene.render(null, engine.getScreenBuffer());
		return true;
	}

	@Override
	public boolean shutdown(Engine engine) {
		State.Entities.destroyAll(State);
		return true;
	}

	// Intialization -----------------------------------------------------------
	
	private void loadResources() {
		ResourceManager resources = Engine.getEngine().getResourceManager();
		if(resources.aquireResource("cursor") == null) {
			resources.registerResource(ResourceLoader.loadResourceXML("cursor.xml", resources.getFileScanner()));
		}
		if(resources.aquireResource("font") == null) {
			resources.registerResource(ResourceLoader.loadResourceXML("font.xml", resources.getFileScanner()));
		}
	}
	
	private void generateObjects() {
		State.Entities.spawn(new MainMenu(), State);
	}
}
